package com.huike.game.Activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game16;
import com.huike.game.base.Game16BusDesc;

import org.json.JSONObject;

import java.util.Collections;
import java.util.Comparator;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivity16 extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private ExpandableListView el;
    private RequestQueue queue;
    private ProgressDialog dialog;
    private Button btn_game16_Bus_desc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game16);
        initView();
        show();
        initData();



        //详细按钮点击
        btn_game16_Bus_desc.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                //得到901公交车承载数量
                getBusData();



            }
        });


    }

    private void getBusData() {

        show();

        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "busSelect901", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                Gson gson = new Gson();
                Game16BusDesc   busDesc = gson.fromJson(jsonObject.toString(), Game16BusDesc.class);

                hide();
                AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity16.this);
                final AlertDialog dialog = builder.create();
                View inflate = View.inflate(GameActivity16.this, R.layout.game16_alert, null);
                dialog.setView(inflate);

                Button button = inflate.findViewById(R.id.but_game16_close);
                ListView listView = inflate.findViewById(R.id.lv_game16);
                TextView textView = inflate.findViewById(R.id.tv_game16_sum);
                int sum = 0;
                for (int i= 0;i<busDesc.getData().size(); i++){
                    sum += busDesc.getData().get(i).getPeople();
                }

                textView.setText(sum + "");

                AdapterLive live = new AdapterLive(busDesc);
                listView.setAdapter(live);


                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });

                dialog.show();

            }
        }, null);

        queue.add(request);


    }

    private void show() {
        dialog = ProgressDialog.show(this, "提示", "加载中...");
        dialog.show();

    }

    private void hide() {
        dialog.dismiss();
    }

    private void initData() {
        queue = Volley.newRequestQueue(this);

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, Api.api + "busDistances", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("----", jsonObject.toString());

                Gson gson = new Gson();
                Game16 json = gson.fromJson(jsonObject.toString(), Game16.class);

                Collections.sort(json.getData().get(0).getList(), new Comparator<Game16.DataBean.ListBean>() {
                    @Override
                    public int compare(Game16.DataBean.ListBean listBean, Game16.DataBean.ListBean t1) {

                        //字符串截取
                        String result = listBean.getDistances(); //距离站台404米
                        String rel = result.substring(result.lastIndexOf("台") + 1,result.lastIndexOf("米"));

                        String resultq = t1.getDistances(); //距离站台404米
                        String relq = resultq.substring(resultq.lastIndexOf("台") + 1,resultq.lastIndexOf("米"));

                        return Integer.parseInt(rel) - Integer.parseInt(relq);
                    }
                });

                Collections.sort(json.getData().get(1).getList(), new Comparator<Game16.DataBean.ListBean>() {
                    @Override
                    public int compare(Game16.DataBean.ListBean listBean, Game16.DataBean.ListBean t1) {

                        //字符串截取
                        String result = listBean.getDistances(); //距离站台404米
                        String rel = result.substring(result.lastIndexOf("台") + 1,result.lastIndexOf("米"));

                        String resultq = t1.getDistances(); //距离站台404米
                        String relq = resultq.substring(resultq.lastIndexOf("台") + 1,resultq.lastIndexOf("米"));

                        return Integer.parseInt(rel) - Integer.parseInt(relq);
                    }
                });


                ELAdapter adapter = new ELAdapter(json);
                el.setAdapter(adapter);
                el.expandGroup(0);//默认展开第一个 要在setAdapter 后面操作
                hide();
            }
        }, null);


        queue.add(request);

    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        el = findViewById(R.id.el_game16);
        btn_game16_Bus_desc = findViewById(R.id.btn_game16_Bus_desc);


        iv_rigth_image.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);

        tv_content_title.setText("公交车查询");

        tv_left_text.setVisibility(View.GONE);
        iv_left_image.setImageResource(R.drawable.tiao);

    }

    class ELAdapter extends BaseExpandableListAdapter {
        Game16 json;

        public ELAdapter(Game16 json) {
            this.json = json;
        }

        @Override
        public int getGroupCount() {
            return json.getData().size();
        }

        @Override
        public int getChildrenCount(int i) {
            return json.getData().get(i).getList().size();
        }

        @Override
        public Object getGroup(int i) {
            return null;
        }

        @Override
        public Object getChild(int i, int i1) {
            return null;
        }

        @Override
        public long getGroupId(int i) {
            return 0;
        }

        @Override
        public long getChildId(int i, int i1) {
            return 0;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (view == null) {
                view = View.inflate(GameActivity16.this, R.layout.game16_paren, null);
                holder = new ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            if (b) {
                holder.iv_game16_switch.setImageResource(R.drawable.close);
            } else {
                holder.iv_game16_switch.setImageResource(R.drawable.open);
            }

            String title = json.getData().get(i).getTitle();
            holder.tv_game16_way.setText(title);


            return view;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (view == null) {
                view = View.inflate(GameActivity16.this, R.layout.game16_son, null);
                holder = new ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            String distances = json.getData().get(i).getList().get(i).getDistances();

            String mi = distances.substring(distances.lastIndexOf("台") + 1, distances.lastIndexOf("米"));
            int time = Integer.parseInt(mi);
            int par = 333;
            int val = (time / par);

            if (val == 0) {
                holder.tv_game16_time.setText("1分钟内到达");

            } else {
                holder.tv_game16_time.setText(val + "分钟到达");

            }

            holder.tv_game16_distances.setText(json.getData().get(i).getList().get(i1).getDistances());
            holder.tv_game16_busName.setText(json.getData().get(i).getList().get(i1).getBusNumber() + "");


            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }

    }

    class ViewHolder {
        public View rootView;
        public ImageView iv_game16_switch;
        public TextView tv_game16_way;
        public TextView tv_game16_busName;
        public TextView tv_game16_time;
        public TextView tv_game16_distances;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.iv_game16_switch = (ImageView) rootView.findViewById(R.id.iv_game16_switch);
            this.tv_game16_way = (TextView) rootView.findViewById(R.id.tv_game16_way);
            this.tv_game16_busName = (TextView) rootView.findViewById(R.id.tv_game16_busName);
            this.tv_game16_time = (TextView) rootView.findViewById(R.id.tv_game16_time);
            this.tv_game16_distances = (TextView) rootView.findViewById(R.id.tv_game16_distances);
        }

    }

    class AdapterLive extends BaseAdapter {
        Game16BusDesc busDesc;

        public AdapterLive(Game16BusDesc busDesc) {
            this.busDesc = busDesc;
        }

        @Override
        public int getCount() {
            return busDesc.getData().size();
        }

        @Override
        public Object getItem(int i) {
            return busDesc.getData().get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolderAlert holder = null;
            if (view == null) {
                view = View.inflate(GameActivity16.this, R.layout.game16_alert_lv, null);
                holder = new ViewHolderAlert(view);
                view.setTag(holder);

            } else {
                holder = (ViewHolderAlert) view.getTag();
            }

            Game16BusDesc.DataBean bean = busDesc.getData().get(i);
            holder.tv_game16_alert_num.setText(i + 1 + "");
            holder.tv_game16_alert_busNum.setText(bean.getNumber() +"");
            holder.tv_game16_alert_people.setText(bean.getPeople() +"");

//            如果承载人数大于200 字体变红
            if(bean.getPeople() > 200){
                holder.tv_game16_alert_people.setTextColor(Color.parseColor("#ffc53929"));
            }else{
                holder.tv_game16_alert_people.setTextColor(Color.parseColor("#ff000000"));

            }


            return view;
        }

        class ViewHolderAlert {
            public View rootView;
            public TextView tv_game16_alert_num;
            public TextView tv_game16_alert_busNum;
            public TextView tv_game16_alert_people;

            public ViewHolderAlert(View rootView) {
                this.rootView = rootView;
                this.tv_game16_alert_num = (TextView) rootView.findViewById(R.id.tv_game16_alert_num);
                this.tv_game16_alert_busNum = (TextView) rootView.findViewById(R.id.tv_game16_alert_busNum);
                this.tv_game16_alert_people = (TextView) rootView.findViewById(R.id.tv_game16_alert_people);
            }

        }
    }
}
